package com.atguigu.guli.service.statistics.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.statistics.entity.Daily;
import com.atguigu.guli.service.statistics.entity.vo.DailyVo;
import com.atguigu.guli.service.statistics.feign.ucenter.MemberFeign;
import com.atguigu.guli.service.statistics.mapper.DailyMapper;
import com.atguigu.guli.service.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.Authorization;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-08-16
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    MemberFeign memberFeign;
    /*
    每天生成报表信息
     */
    @Override
    public void genDaily(String day) {
        Daily daily = new Daily();
        daily.setLoginNum(RandomUtils.nextInt(10, 100));
        daily.setCourseNum(RandomUtils.nextInt(1, 5));
        daily.setVideoViewNum(RandomUtils.nextInt(100, 1000));
        daily.setDateCalculated(day);
        R registNum = memberFeign.getRegistNum(day);
        //远程调用注册信息
        daily.setRegisterNum((Integer) registNum.getData().get("data"));
        //插入到数据库中
        baseMapper.insert(daily);
    }

    @Override
    public DailyVo getStatistics(String start, String end) {
        LambdaQueryWrapper<Daily> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(Daily::getDateCalculated,start);
        queryWrapper.le(Daily::getDateCalculated,end);
        queryWrapper.orderByAsc(Daily::getDateCalculated);
        List<Daily> dailies = baseMapper.selectList(queryWrapper);
        DailyVo dailyVo = new DailyVo();
        dailies.forEach(item -> {
            dailyVo.getDates().add(item.getDateCalculated());
            dailyVo.getCourseNumList().add(item.getCourseNum());
            dailyVo.getLoginNumList().add(item.getLoginNum());
            dailyVo.getRegistNumList().add(item.getRegisterNum());
            dailyVo.getVideoViewNumList().add(item.getVideoViewNum());
        });
        return dailyVo;
    }
}
